
package com.bbh.plugin.single.privilege.demo.controller;


import com.bbh.plugin.single.privilege.core.entity.Rule;
import com.bbh.plugin.single.privilege.core.service.RuleService;
import com.bbh.common.base.vo.GridVo;
import com.bbh.common.domain.Condition;
import com.bbh.common.domain.Message;
import com.bbh.common.domain.StatusType;
import com.bbh.common.utils.IdUtils64;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.bbh.common.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;

/**
 * Controller - 菜单
 *
 * @author KCSHOP Team
 * @version 3.0
 */
@Controller
@RequestMapping("/rule")
public class  RuleController extends BasicController {
    @Autowired
    private RuleService ruleService;
    /**
     * 跳转菜单页面
     * @author bestgwd
     *
     */
    @RequestMapping(VIEW_FILED+"/index")
    public String index(ModelMap model) {
        return "single/rule/index";
    }

    @RequestMapping(value = INSERT_FILED+"/insert")
    public
    @ResponseBody
    Message insert(@Valid Rule rule) {
        Date date =new Date();
        rule.setCreateTime(date);
        rule.setModifyTime(date);
        rule.setCreateBy(getLoginName());
        rule.setModifyBy(getLoginName());

        rule.setId(IdUtils64.getInstance().getStringId());
        ruleService.insert(rule);
        return new Message(StatusType.SUCCESS.getKey(), "保存成功");
    }

    @RequestMapping(VIEW_FILED+"/list")
    @ResponseBody
    public GridVo list(HttpServletRequest request, Model model,String code,String expression,String name) throws ServletRequestBindingException {
        Condition condition=new Condition();
        if (StringUtils.isNoneBlank(name)) {
            condition.put("name",Condition.LIKE, name);
        }
        if (StringUtils.isNoneBlank(expression)) {
            condition.put("expression",Condition.LIKE, expression);
        }
        if (StringUtils.isNoneBlank(code)) {
            condition.put("code",Condition.LIKE, code);
        }
        Page<Rule> pageResult=ruleService.selectPage(condition,null,initPageRequest(request, PAGE_NUMBER, PAGE_ROWS));
        return  new GridVo(pageResult);
    }

    /**
     * 跳转到edit页面
     *
     * @return
     */
    @RequestMapping(UPDATE_FILED+"/edit")
    public String toEdit(String id, Model model, String type) {
        Rule rule;
        if ("edit".equals(type)) {
            rule=ruleService.selectByPrimaryKey(id);
            model.addAttribute("rule", rule);
            model.addAttribute(OPERATE_TYPE, "edit");
        }
        if ("add".equals(type)) {
            model.addAttribute(OPERATE_TYPE, "add");
        }
        return  "single/rule/edit";
    }
    /**
     * update
     *
     * @return
     */
    @RequestMapping(UPDATE_FILED+"/update")
    public @ResponseBody
    Message update(@Valid Rule rule) {
        Assert.notNull(rule.getId(), "参数错误");
        Rule updateRule = ruleService.selectByPrimaryKey(rule.getId());
        if (updateRule != null) {
            updateRule.setName(rule.getName());
            updateRule.setCode(rule.getCode());
            updateRule.setExpression(rule.getExpression());
            updateRule.setDescription(rule.getDescription());
            updateRule.setModifyBy(getLoginName());
            updateRule.setModifyTime(new Date());
            ruleService.updateByPrimaryKeySelective(updateRule);
        }
        return new Message(StatusType.SUCCESS.getKey(), "更新成功");
    }

    @RequestMapping(DELETE_FILED+"/delete")
    public
    @ResponseBody
    Message delete(@RequestParam(value = "ids[]") String[] ids) {
        ruleService.delete(ids);
        Message message = new Message(StatusType.SUCCESS.getKey(), "删除成功");
        return message;
    }






}